package com.gable.controller;

import com.alibaba.fastjson.JSON;
import com.gable.model.User;
import com.gable.service.UserService;
import com.gable.util.MessageUtil;
import com.gable.util.ReturnResultUtil;
import com.gable.util.WebSocketJoin;
import com.gable.util.WebSocketUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.Session;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by sjx on 2018/5/7.
 */

@Controller
@RequestMapping("/screen")
public class ScreenController {
    private final Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    UserService userService;

    /**
     * 查询大屏计数信息
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "queryList", method = RequestMethod.POST)
    @ResponseBody
    public ReturnResultUtil queryList(HttpServletRequest request, HttpServletResponse response) {
        try{
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user.getServiceProviderId()==null){
                return new ReturnResultUtil(500, MessageUtil.LOGINERROR);
            }
            Map map = new HashMap();
            map.put("serviceProviderId",user.getServiceProviderId());
            ReturnResultUtil rru = userService.queryList(map);
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500, MessageUtil.QUERYEXCEPTION);
    }

    /**
     * 查询待维修数据
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "queryRepairList", method = RequestMethod.POST)
    @ResponseBody
    public ReturnResultUtil queryRepairList(HttpServletRequest request, HttpServletResponse response) {
        try{
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user.getServiceProviderId()==null){
                return new ReturnResultUtil(500, MessageUtil.PARAMETERERROR);
            }
            Map map = new HashMap();
            map.put("serviceProviderId",user.getServiceProviderId());
            ReturnResultUtil rru = userService.queryRepairList(map);
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500, MessageUtil.QUERYEXCEPTION);
    }

    /**
     * 查询待保养数据
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "queryUpkeepList", method = RequestMethod.POST)
    @ResponseBody
    public ReturnResultUtil queryUpkeepList(HttpServletRequest request, HttpServletResponse response) {
        try{
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user.getServiceProviderId()==null){
                return new ReturnResultUtil(500, MessageUtil.PARAMETERERROR);
            }
            Map map = new HashMap();
            map.put("serviceProviderId",user.getServiceProviderId());
            ReturnResultUtil rru = userService.queryUpkeepList(map);
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500, MessageUtil.QUERYEXCEPTION);
    }

    /**
     * 查询待巡检数据
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "queryInspectionList", method = RequestMethod.POST)
    @ResponseBody
    public ReturnResultUtil queryInspectionList(HttpServletRequest request, HttpServletResponse response) {
        try{
            User user = (User)request.getSession().getAttribute("loginUser");
            if(user.getServiceProviderId()==null){
                return new ReturnResultUtil(500, MessageUtil.PARAMETERERROR);
            }
            Map map = new HashMap();
            map.put("serviceProviderId",user.getServiceProviderId());
            ReturnResultUtil rru = userService.queryInspectionList(map);
            logger.info(JSON.toJSONString(rru));
            return rru;
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500, MessageUtil.QUERYEXCEPTION);
    }

    /**
     *发送信息
     * @param request
     * @param response
     * @param message
     * @return
     */
    @RequestMapping(value = "showMessage", method = RequestMethod.POST)
    @ResponseBody
    public void showMessage(HttpServletRequest request, HttpServletResponse response,String message) {
        Set<Session> sessionSet = WebSocketJoin.sessionSet;
        WebSocketUtil.sendMessage(sessionSet,message);
    }

    /**
     * 天气预报
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "queryWeather", method = RequestMethod.GET)
    @ResponseBody
    public ReturnResultUtil queryWeather(HttpServletRequest request, HttpServletResponse response) {
        try{
//            String city = java.net.URLEncoder.encode("广州", "utf-8");
            //拼地址
            String apiUrl = String.format("http://t.weather.sojson.com/api/weather/city/101280101");
            //开始请求
            URL url= new URL(apiUrl);
            URLConnection open = url.openConnection();
            InputStream input = open.getInputStream();
            //这里转换为String，带上包名，怕你们引错包
            String result = org.apache.commons.io.IOUtils.toString(input,"utf-8");
            return new ReturnResultUtil(500, MessageUtil.QUERYSUCCESS,result);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new ReturnResultUtil(500, MessageUtil.QUERYEXCEPTION);
    }
}
